<?php

namespace app\worker\controller;

use app\common\model\mysql\OrderBackBucket;
use app\common\model\mysql\OrderBackTicket;
use app\common\model\mysql\OrderProductNormal;
use app\common\model\mysql\OrderProductReturn;
use app\common\model\mysql\OrderProductWater;
use app\common\model\mysql\OrderTask;
use app\common\model\mysql\OrderTicket;
use app\common\model\mysql\Product;
use app\common\model\mysql\Send;
use app\common\business\Order as OrderBus;
use app\common\model\mysql\Order as OrderModel;
use app\common\model\mysql\Ticket;
use think\Collection;

class Order extends AuthBase
{
    /**
     * 未完成订单列表
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function unfinishedOrderList(){
        $page = $this->request->param('pageNum', 1);//当前页
        $limit = $this->request->param('pageSize', 10);//每页显示条数
        if(empty($page) || empty($limit)){
            return show(config("status.error"),"缺少分页参数");
        }
        $map = [
            ['worker_id_send','=',$this->workerId],
            ['stateenum','<',15],
            ['isdel','=','0']
        ];
        $sendPassOrder = (new Send())->getSendFind($map);
        // 构建订单查询条件
        $orderWhere = [
            ['company_id','=',$this->companyId],
            ['shop_id','=',$this->shopId],
            ['worker_id_send','=',$this->workerId],
            ['ordertypeenum','=',1],
            ['iscancel','=',0],
            ['isdel','=','0'],
            ['stateenum','in',[2, 4, 64]],
        ];
        if($sendPassOrder){
            $orderWhere[] = ['send_id', '>', 0];
        }
        $field = "order_id,order_sn,typeitemenum,allotdate,stateenum,customer_address,urgent_count,create_time,longitude,latitude,is_read,customer_phone,iscancel,worker_remark";
        $orderDatas = (new OrderBus())->getNormalOrderList($orderWhere,$limit,$page,$field);
        if(empty($orderDatas['data'])){
            $orderData = [];
        } else{
            $OrderProductWaterDb = (new OrderProductWater());
            $OrderTicketDb = (new OrderTicket());
            $OrderProductNormalDb = (new OrderProductNormal());
            $OrderTaskDb = (new OrderTask());
            foreach ($orderDatas['data'] as $key=>$value){
                $i = 0;
                $goodsData = [];
                $where = [
                    ['order_id','=',$value['order_id']]
                ];
                //订单售水
                $field = "product_name,sellcount_real as sellcount,selltypeenum,paytypeenum";
                $orderWaterData = $OrderProductWaterDb->getNormalOrderProductWater($where,$field);
                if($orderWaterData){
                    foreach ($orderWaterData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['product_name'];
                        $goodsData[$i]['sellcount']     = $v['sellcount'];
                        $goodsData[$i]['givenumber']    = 0;
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 1;
                        $goodsData[$i]['paytypeenum']   = $v['paytypeenum'];
                        $i++;
                    }
                }

                //订单售水票
                $field = "ticket_name,count_cost,count_give,selltypeenum";
                $OrderTicketData = $OrderTicketDb->getNormalOrderTicket($where,$field);
                if($OrderTicketData){
                    foreach ($OrderTicketData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['ticket_name'];
                        $goodsData[$i]['sellcount']     = $v['count_cost'];
                        $goodsData[$i]['givenumber']    = $v['count_give'];
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 2;
                        $goodsData[$i]['paytypeenum']   = 0;
                        $i++;
                    }
                }

                //订单售普通商品
                $field = "product_name,sellcount,selltypeenum,paytypeenum";
                $OrderProductNormalData = $OrderProductNormalDb->getNormalOrderProductNormal($where,$field);
                if($OrderProductNormalData){
                    foreach ($OrderProductNormalData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['product_name'];
                        $goodsData[$i]['sellcount']     = $v['sellcount'];
                        $goodsData[$i]['givenumber']    = 0;
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 3;
                        $goodsData[$i]['paytypeenum']   = $v['paytypeenum'];
                        $i++;
                    }
                }
                //订单任务
                $OrderTaskData = $OrderTaskDb->getNormalOrderTaskFiledArr($where,'tname');
                if(empty($OrderTaskData)){
                    $OrderTaskData = [];
                }
                $phoneOr = explode(',', $value['customer_phone']);
                $orderData[] = [
                    'order_id'      => $value['order_id'],
                    'order_sn'      => $value['order_sn'],
                    'typeitemenum'  => $value['typeitemenum'],
                    'allotdate'     => $value['allotdate'] ? date("Y-m-d H:i",strtotime($value['allotdate'])) : "" ,
                    'stateenum'     => $value['stateenum'],
                    'phone'         => $phoneOr[0],
                    'address'       => $value['customer_address'],
                    'urgent_count'  => $value['urgent_count'],
                    'adddate'       => date("Y-m-d H:i",strtotime($value['create_time'])),
                    'longitude'     => $value['longitude']??'',
                    'latitude'      => $value['latitude']??'',
                    'taskName'      => $OrderTaskData,
                    'is_read'       => $value['is_read'],
                    'goodsData'     => $goodsData,
                    'worker_remark' => $value['worker_remark']??'',
                    'iscancel'      => $value['iscancel'],
                ];
            }
        }
        return show(config('status.success'), "未完成订单列表", $orderData);
    }

    /**
     * 已完成订单列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function finishedOrderList(){
        $page = $this->request->param('pageNum', 1);//当前页
        $limit = $this->request->param('pageSize', 10);//每页显示条数
        if(empty($page) || empty($limit)){
            return show(config("status.error"),"缺少分页参数");
        }
        // 构建订单查询条件
        $orderWhere = [
            ['company_id','=',$this->companyId],
            ['shop_id','=',$this->shopId],
            ['worker_id_send','=',$this->workerId],
            ['ordertypeenum','=',1],
            ['iscancel','=',0],
            ['isdel','=','0'],
            ['stateenum','in',[8, 16]],
        ];
        $field = "order_id,order_sn,typeitemenum,allotdate,stateenum,customer_address,urgent_count,create_time,longitude,latitude,is_read,customer_phone,iscancel,worker_remark";
        $orderDatas = (new OrderBus())->getNormalOrderList($orderWhere,$limit,$page,$field);
        if(empty($orderDatas['data'])){
            $orderData = [];
        } else {
            $OrderProductWaterDb = (new OrderProductWater());
            $OrderTicketDb = (new OrderTicket());
            $OrderProductNormalDb = (new OrderProductNormal());
            $OrderTaskDb = (new OrderTask());
            foreach ($orderDatas['data'] as $key=>$value){
                $i = 0;
                $goodsData = [];
                $where = [
                    ['order_id','=',$value['order_id']]
                ];
                //订单售水
                $field = "product_name,sellcount_real as sellcount,selltypeenum,paytypeenum";
                $orderWaterData = $OrderProductWaterDb->getNormalOrderProductWater($where,$field);
                if($orderWaterData){
                    foreach ($orderWaterData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['product_name'];
                        $goodsData[$i]['sellcount']     = $v['sellcount'];
                        $goodsData[$i]['givenumber']    = 0;
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 1;
                        $goodsData[$i]['paytypeenum']   = $v['paytypeenum'];
                        $i++;
                    }
                }

                //订单售水票
                $field = "ticket_name,count_cost,count_give,selltypeenum";
                $OrderTicketData = $OrderTicketDb->getNormalOrderTicket($where,$field);
                if($OrderTicketData){
                    foreach ($OrderTicketData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['ticket_name'];
                        $goodsData[$i]['sellcount']     = $v['count_cost'];
                        $goodsData[$i]['givenumber']    = $v['count_give'];
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 2;
                        $goodsData[$i]['paytypeenum']   = 0;
                        $i++;
                    }
                }

                //订单售普通商品
                $field = "product_name,sellcount,selltypeenum,paytypeenum";
                $OrderProductNormalData = $OrderProductNormalDb->getNormalOrderProductNormal($where,$field);
                if($OrderProductNormalData){
                    foreach ($OrderProductNormalData as $k=>$v){
                        $goodsData[$i]['goodsname']     = $v['product_name'];
                        $goodsData[$i]['sellcount']     = $v['sellcount'];
                        $goodsData[$i]['givenumber']    = 0;
                        $goodsData[$i]['selltypeenum']  = $v['selltypeenum'];
                        $goodsData[$i]['type']          = 3;
                        $goodsData[$i]['paytypeenum']   = $v['paytypeenum'];
                        $i++;
                    }
                }
                //订单任务
                $OrderTaskData = $OrderTaskDb->getNormalOrderTaskFiledArr($where,'tname');
                if(empty($OrderTaskData)){
                    $OrderTaskData = [];
                }
                $phoneOr = explode(',', $value['customer_phone']);
                $orderData[] = [
                    'order_id'      => $value['order_id'],
                    'order_sn'      => $value['order_sn'],
                    'typeitemenum'  => $value['typeitemenum'],
                    'allotdate'     => $value['allotdate'] ? date("Y-m-d H:i",strtotime($value['allotdate'])) : "" ,
                    'stateenum'     => $value['stateenum'],
                    'phone'         => $phoneOr[0],
                    'address'       => $value['customer_address'],
                    'urgent_count'  => $value['urgent_count'],
                    'adddate'       => date("Y-m-d H:i",strtotime($value['create_time'])),
                    'longitude'     => $value['longitude']??'',
                    'latitude'      => $value['latitude']??'',
                    'taskName'      => $OrderTaskData,
                    'is_read'       => $value['is_read'],
                    'goodsData'     => $goodsData,
                    'worker_remark' => $value['worker_remark']??'',
                    'iscancel'      => $value['iscancel'],
                ];
            }
        }
        return show(config('status.success'), "已完成订单列表", $orderData);
    }

    /**
     * 订单详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderDetails(){
        $orderId = $this->request->param("order_id","","trim");
        if(empty($orderId)){
            return show(config("status.error"),"缺少参数订单id");
        }
        $where = [
            ['order_id','=',$orderId]
        ];
        $orData = (new OrderModel())->getOrderFind($where);
        $orderData = [
            'order_id' 		=> $orData['order_id'],
            'order_sn' 		=> $orData['order_sn'],
            'typeitemenum' 	=> $orData['typeitemenum'],
            'allotdate' 	=> $orData['allotdate'] ? date("Y-m-d H:i",strtotime($orData['allotdate'])) : "" ,
            'stateenum' 	=> $orData['stateenum'],
            'address' 		=> $orData['customer_address'],
            'phone'			=> explode(',', $orData['customer_phone'])[0],
            'urgent_count' 	=> $orData['urgent_count'],
            'longitude'		=> $orData['longitude']??'',
            'latitude'		=> $orData['latitude']??'',
            'amount'		=> $orData['amount'],
            'paid_in'		=> $orData['paid_in'],
            'arrears'		=> $orData['arrears'],
            'paytypeenum'	=> $orData['paytypeenum'],
            'senddate' 		=> $orData['senddate'] ? date("Y-m-d H:i",strtotime($orData['senddate'])) : "" ,
            'adddate' 		=> $orData['create_time'] ? date("Y-m-d H:i",strtotime($orData['create_time'])) : "" ,
            'price_diff_bucket'=> $orData['price_diff_bucket'],
            'price_diff_ticket'=> $orData['price_diff_ticket'],
            'price_sell_water_should'	=> $orData['price_sell_water_should'],
            'price_sell_product_should'	=> $orData['price_sell_product_should'],
            'price_sell_ticket_should'	=> $orData['price_sell_ticket_should'],
            'price_pledge_bucket'		=> $orData['price_pledge_bucket'],
            'price_owe_sell_bucket'		=> $orData['price_owe_sell_bucket'],
            'price_oew_sell_ticket'		=> $orData['price_owe_sell_ticket'],
            'price_owe_pledge_bucket'	=> $orData['price_owe_pledge_bucket'],
            'price_owe_back'			=> $orData['price_owe_back'],
            'worker_remark'				=> $orData['worker_remark'],
            'price_sell_ticketmoney'	=> $orData['price_sell_ticketmoney'],
            'price_sell_bucket'			=> $orData['price_sell_bucket'],
            'price_bucket_return'		=> $orData['price_bucket_return'],
            'is_still_bucket_ticket'	=> $orData['is_still_bucket_ticket'],
        ];
        //订单售普通商品
        $proWhere = [
            ['order_id','=',$orderId],
            ['selltypeenum','<>',4],
        ];
        $field = "order_product_normal_id,product_name,price_costunit,price_sellunit,price_selltotal,sellcount,selltypeenum,paytypeenum,tk_ticketid,tk_ticketname,tk_ticket_count,sellcount_real";
        $productData = (new OrderProductNormal())->getNormalOrderProductNormal($proWhere,$field);
        if($productData){
            foreach ($productData as $key => $value) {
                if($value['selltypeenum'] == 2){
                    $productData[$key]['price_sellunit'] = 0.00;
                    $productData[$key]['price_selltotal'] = 0.00;
                }
                if($value['paytypeenum'] > 1){
                    $productData[$key]['tk_ticketname'] = "";
                    $productData[$key]['tk_ticket_count'] = 0;
                }
            }
        }
        //订单售水
        $waterWhere = [
            ['order_id','=',$orderId],
            ['selltypeenum','<>',4],
        ];
        $field = "order_product_water_id,product_id,product_name,sellcount,sellcount_real,bucket_value,ticket_value,bucketback_count_should,bucketback_count_real,tk_ticketcount_should,tk_ticketcount_real,product_code,paytypeenum,isdifference,selltypeenum,price_unit,price_diff_ticket_self,company_id";
        $goodsData = (new OrderProductWater())->getNormalOrderProductWater($waterWhere,$field);
        if($goodsData){
            foreach ($goodsData as $k=>$v){
                $proWhere = [
                    ['typeenum','=',4],
                    ['parentid','=',$v['product_id']],
                    ['isdel','=','0'],
                ];
                $bucketData = (new Product())->getProductFind($proWhere);
                $goodsData[$k]['bucket_value'] = $bucketData['bucket_price_pledge'];
                $goodsData[$k]['ticket'] = array(
                    'ticket_id'		    => 0,
                    'tk_ticketname'	    => '',
                    'tk_ticketcount'    => 0,
                    'diff_price_real'   => 0.00,
                );
                if($v['paytypeenum'] == 1){
                    $ticketWhere = [
                        ['product_code','=',$v['product_code']],
                        ['company_id','=',$v['company_id']],
                        ['isdel','=','0'],
                    ];
                    $ticketData = (new Ticket())->getTicketFind($ticketWhere,"id as ticket_id,tname as ticket_name,price_unit as ticket_price");
                    if($orData['stateenum']<7 || $orData['stateenum'] == 64){
                        $goodsData[$k]['ticket'] = array(
                            'ticket_id'		=>$ticketData['ticket_id'],
                            'tk_ticketname'	=>$ticketData['ticket_name'],
                            'tk_ticketcount'=>$goodsData[$k]['sellcount_real']??'0',
                            'diff_price_real'=>$goodsData[$k]['sellcount_real'] == "0" ? '0.00' : (string)($ticketData['ticket_price']/$goodsData[$k]['sellcount_real']),
                        );
                    }
                }
                if($orData['stateenum']<7 || $orData['stateenum'] == 64){
                    $goodsData[$k]['bucket'] = array(
                        'product_id'	=>$bucketData['product_id'],
                        'product_name'	=>$bucketData['product_name'],
                        'tcount'		=>$goodsData[$k]['sellcount_real'],
                        'bucket_value'	=>$bucketData['price_unit']??"0.00",
                        'producttypeenum'=>"4",
                    );
                }
                //回桶
                $backBucketWhere = [
                    ['order_product_water_id','=',$v['order_product_water_id']]
                ];
                $field = "id as order_back_bucket_id,product_name,product_id,tcount,bucket_value,price_unit,pricetotal_real,back_buckettypeenum,producttypeenum";
                $backBucketData = (new OrderBackBucket())->getNormalBackBucket($backBucketWhere,$field);
                if($backBucketData){
                    foreach ($backBucketData as $key => $value) {
                        if($key == 0){
                            $a = $b = $c = $d = $e = $f = $g = $h = $j = 0;
                            $goodsData[$k]['buckItem1'] = $goodsData[$k]['buckItem2'] = $goodsData[$k]['buckItem3'] = $goodsData[$k]['buckItem4'] = $goodsData[$k]['buckItem5'] = $goodsData[$k]['buckItem6'] = $goodsData[$k]['buckItem7'] = $goodsData[$k]['buckItem8'] = $goodsData[$k]['buckItem9'] = [];
                        }
                        if ($value['back_buckettypeenum'] == 1) {
                            $goodsData[$k]['buckItem1'][$a]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem1'][$a]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem1'][$a]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem1'][$a]['bucket_value'] = $value['bucket_value'] ?? "0.00";
                            $goodsData[$k]['buckItem1'][$a]['producttypeenum'] = $value['producttypeenum'];
                            $a++;
                            if ($orData['stateenum'] > 7 || $orData['stateenum'] < 64) {
                                $goodsData[$k]['bucket'] = array(
                                    'product_id' => $value['product_id'],
                                    'product_name' => $value['product_name'],
                                    'tcount' => $value['tcount'],
                                    'bucket_value' => $value['bucket_value'] ?? "0.00",
                                    'producttypeenum' => $value['producttypeenum'],
                                );
                            }
                        }elseif($value['back_buckettypeenum'] == 2){
                            $goodsData[$k]['buckItem2'][$b]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem2'][$b]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem2'][$b]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem2'][$b]['bucket_value'] = $value['price_unit']??"0.00";
                            $goodsData[$k]['buckItem2'][$b]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $b++;
                        }elseif($value['back_buckettypeenum'] == 4){
                            $goodsData[$k]['buckItem3'][$c]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem3'][$c]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem3'][$c]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem3'][$c]['bucket_value'] = $value['bucket_value']??"0.00";
                            $goodsData[$k]['buckItem3'][$c]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $c++;
                        }elseif($value['back_buckettypeenum'] == 8){
                            $goodsData[$k]['buckItem4'][$d]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem4'][$d]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem4'][$d]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem4'][$d]['bucket_value'] = $value['bucket_value']??"0.00";
                            $goodsData[$k]['buckItem4'][$d]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $d++;
                        }elseif($value['back_buckettypeenum'] == 16){
                            $goodsData[$k]['buckItem5'][$e]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem5'][$e]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem5'][$e]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem5'][$e]['bucket_value'] = $value['bucket_value']??"0.00";
                            $goodsData[$k]['buckItem5'][$e]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $e++;
                        }elseif($value['back_buckettypeenum'] == 32){
                            $goodsData[$k]['buckItem6'][$f]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem6'][$f]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem6'][$f]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem6'][$f]['bucket_value'] = $value['price_unit']??"0.00";
                            $goodsData[$k]['buckItem6'][$f]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $f++;
                        }elseif($value['back_buckettypeenum'] == 64){
                            $goodsData[$k]['buckItem7'][$g]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem7'][$g]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem7'][$g]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem7'][$g]['bucket_value'] = $value['bucket_value']??"0.00";
                            $goodsData[$k]['buckItem7'][$g]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $g++;
                        }elseif($value['back_buckettypeenum'] == 128){
                            $goodsData[$k]['buckItem8'][$h]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem8'][$h]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem8'][$h]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem8'][$h]['bucket_value'] = $value['bucket_value']??"0.00";
                            $goodsData[$k]['buckItem8'][$h]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $h++;
                        }elseif($value['back_buckettypeenum'] == 256){
                            $goodsData[$k]['buckItem9'][$j]['product_id'] = $value['product_id'];
                            $goodsData[$k]['buckItem9'][$j]['product_name'] = $value['product_name'];
                            $goodsData[$k]['buckItem9'][$j]['tcount'] = $value['tcount'];
                            $goodsData[$k]['buckItem9'][$j]['bucket_value'] = $value['price_unit']??"0.00";
                            $goodsData[$k]['buckItem9'][$j]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                            $j++;
                        }
                    }
                }
                //回票
                $backTicketWhere = [
                    ['order_product_water_id','=',$v['order_product_water_id']]
                ];
                $field = "id as order_back_ticket_id,ticket_id,ticket_name as tk_ticketname,ticket_count_should as tk_ticketcount_should,ticket_count as tk_ticketcount,diff_price_should,diff_price_real,back_tickettypeenum,ticket_value";
                $backTicketData = (new OrderBackTicket())->getNormalBackTicket($backTicketWhere,$field);
                if($backTicketData){
                    foreach ($backTicketData as $key => $value) {
                        if($key == 0){
                            $goodsData[$k]['tickItem1'] = $goodsData[$k]['tickItem2'] = $goodsData[$k]['tickItem3'] = $goodsData[$k]['tickItem4'] = $goodsData[$k]['tickItem5'] = $goodsData[$k]['tickItem6'] = [];
                            $i = $n = $p = $l = $m = $o = 0;
                        }
                        //回票类型，1回票 2欠票 4还欠票  8卖水(卖票) 16欠转卖 32 不正常回票
                        if($value['back_tickettypeenum'] == 1){
                            $goodsData[$k]['tickItem1'][$i]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem1'][$i]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem1'][$i]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem1'][$i]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem1'][$i]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem1'][$i]['diff_price_should'] = $value['diff_price_should'];
                            if($value['tk_ticketcount']>0){
                                $goodsData[$k]['tickItem1'][$i]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            }else{
                                $goodsData[$k]['tickItem1'][$i]['diff_price_real'] = "0";
                            }
                            $i++;
                            if($orData['stateenum']>7 || $orData['stateenum']<64){
                                $goodsData[$k]['ticket'] = array(
                                    'ticket_id'		=>$value['ticket_id'],
                                    'tk_ticketname'	=>$value['tk_ticketname'],
                                    'tk_ticketcount'=>$value['tk_ticketcount']??'0',
                                    'diff_price_real'=>$value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']),
                                );
                            }
                        }elseif($value['back_tickettypeenum'] == 2){
                            $goodsData[$k]['tickItem2'][$n]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem2'][$n]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem2'][$n]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem2'][$n]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem2'][$n]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem2'][$n]['diff_price_should'] = $value['diff_price_should'];
                            $goodsData[$k]['tickItem2'][$n]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            $n++;
                        }elseif($value['back_tickettypeenum'] == 4){
                            $goodsData[$k]['tickItem3'][$p]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem3'][$p]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem3'][$p]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem3'][$p]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem3'][$p]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem3'][$p]['diff_price_should'] = $value['diff_price_should'];
                            $goodsData[$k]['tickItem3'][$p]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            $p++;
                        }elseif($value['back_tickettypeenum'] == 8){
                            $goodsData[$k]['tickItem4'][$l]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem4'][$l]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem4'][$l]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem4'][$l]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem4'][$l]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem4'][$l]['diff_price_should'] = $value['diff_price_should'];
                            $goodsData[$k]['tickItem4'][$l]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            $l++;
                        }elseif($value['back_tickettypeenum'] == 16){
                            $goodsData[$k]['tickItem5'][$m]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem5'][$m]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem5'][$m]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem5'][$m]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem5'][$m]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem5'][$m]['diff_price_should'] = $value['diff_price_should'];
                            $goodsData[$k]['tickItem5'][$m]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            $m++;
                        }elseif($value['back_tickettypeenum'] == 32){
                            $goodsData[$k]['tickItem6'][$o]['order_back_ticket_id'] 	= $value['order_back_ticket_id'];
                            $goodsData[$k]['tickItem6'][$o]['ticket_id']  = $value['ticket_id'];
                            $goodsData[$k]['tickItem6'][$o]['tk_ticketname'] = $value['tk_ticketname'];
                            $goodsData[$k]['tickItem6'][$o]['tk_ticketcount_should'] = $value['tk_ticketcount_should'];
                            $goodsData[$k]['tickItem6'][$o]['tk_ticketcount'] = $value['tk_ticketcount'];
                            $goodsData[$k]['tickItem6'][$o]['diff_price_should'] = $value['diff_price_should'];
                            $goodsData[$k]['tickItem6'][$o]['diff_price_real'] = $value['tk_ticketcount'] == "0" ? '0.00' : (string)($value['ticket_value']/$value['tk_ticketcount']);
                            $o++;
                        }
                    }
                }
            }
        }
        //是否还欠桶还欠票
        if($orData['is_still_bucket_ticket'] == 1){
            // 一次性查询所有相关数据
            $whereBack = [
                ['order_id','=',$orderId]
            ];
            $field = "id, product_id_water as pid, product_name as tname, tcount, bucket_value as price, back_buckettypeenum as back_type";
            $backBucketDatas = (new OrderBackBucket())->getNormalBackBucket($whereBack,$field);

            $field = "id, product_id as pid, ticket_name as tname, ticket_count as tcount, ticket_value as price, back_tickettypeenum as back_type, ticket_id";
            $backTicketDatas = (new OrderBackTicket())->getNormalBackTicket($whereBack,$field);
            // 合并数据并去重
            $allData = array_merge($backBucketDatas, $backTicketDatas);
            $uniqueData = Collection::make($allData)->unique('pid')->toArray();
            // 初始化结果数组
            $ownDatas = [];
            foreach ($uniqueData as $item) {
                $type = ($item['ticket_id'] ?? null) ? '2' : '1'; // 判断是桶票还是门票
                $ownDatas[] = [
                    'name' => $item['tname'],
                    'data' => [
                        [
                            'id' => $item['id'],
                            'tname' => $item['tname'],
                            'tcount' => $item['tcount'],
                            'price' => $item['price'],
                            'back_type' => $item['back_type'],
                            'type' => $type,
                            'pid' => $item['pid'],
                            'ticket_id' => $item['ticket_id'] ?? null, // 门票特有字段，桶票为null
                        ]
                    ]
                ];
            }
            // 如果有多种类型的票（桶票和门票），需要合并相同pid的数据
            $mergedOwnDatas = [];
            foreach ($ownDatas as $data) {
                $pid = $data['data'][0]['pid'];
                if (!isset($mergedOwnDatas[$pid])) {
                    $mergedOwnDatas[$pid] = $data;
                    $mergedOwnDatas[$pid]['data'] = [];
                }
                $mergedOwnDatas[$pid]['data'][] = $data['data'][0];
            }
            $ownDatas = array_values($mergedOwnDatas);
        }else{
            $ownDatas = [];
        }
        //订单购票
        $buyTicketWhere = [
            ['order_id','=',$orderId],
            ['isdel','=','0']
        ];
        $field = "order_ticket_id,ticket_name,count_give,count_cost,price_selltotal,count_real";
        $buyTicket = (new OrderTicket())->getNormalOrderTicket($buyTicketWhere,$field);
        //订单退货
        $returnWhere = [
            ['order_id','=',$orderId],
            ['isdel','=','0']
        ];
        $field = "order_product_return_id,product_id,product_name,tcount,price_diff";
        $returnGoodsData = (new OrderProductReturn())->getNormalOrderProductReturn($returnWhere,$field);
        //订单换货
        $exchangeGoodsWhere = [
            ['order_id','=',$orderId],
            ['selltypeenum','=',4],
            ['isdel','=','0']
        ];
        $field = "order_product_water_id as exchange_id,product_id,product_name,sellcount as tcount";
        $exchangeGoodsDatas = (new OrderProductWater())->getNormalOrderProductWater($exchangeGoodsWhere,$field);
        $exchangeProductWhere = [
            ['order_id','=',$orderId],
            ['selltypeenum','=',4],
            ['isdel','=','0']
        ];
        $field = "order_product_normal_id as exchange_id,product_id,product_name,sellcount as tcount";
        $exchangeProductDatas = (new OrderProductNormal())->getNormalOrderProductNormal($exchangeProductWhere,$field);
        $exchangeGoodsData = array_merge($exchangeGoodsDatas,$exchangeProductDatas);
        //多回桶数据
        $moreBarrelsBackWhere = [
            ['order_id','=',$orderId],
            ['back_buckettypeenum','=',512],
        ];
        $field = "product_id,product_name,tcount";
        $moreBarrelsBackData = (new OrderBackBucket())->getNormalBackBucket($moreBarrelsBackWhere,$field);
        //多回票数据
        $moreTicketBackWhere = [
            ['order_id','=',$orderId],
            ['back_tickettypeenum','=',64],
        ];
        $field = "ticket_id,ticket_name,ticket_count as tcount";
        $moreTicketBackData = (new OrderBackTicket())->getNormalBackTicket($moreTicketBackWhere,$field);
        //订单退桶
        $returnBucketWhere = [
            ['order_id','=',$orderId],
            ['back_buckettypeenum','=',32],
            ['product_id_water','=',0],
        ];
        $field = "id as order_back_bucket_id,product_id,product_name,tcount,price_unit,back_buckettypeenum";
        $returnBucketData = (new OrderBackBucket())->getNormalBackBucket($returnBucketWhere,$field);
        $data = array(
            'orderData'     => $orderData,
            'productData'   => $productData,
            'goodsData'     => $goodsData,
            'ownData'       => $ownDatas,
            'buyTicket'     => $buyTicket,
            'returnGoodsData'       => $returnGoodsData,
            'returnBucketData'      => $returnBucketData,
            'exchangeGoodsData'     => $exchangeGoodsData,
            'moreBarrelsBackData'   => $moreBarrelsBackData,
            'moreTicketBackData'    => $moreTicketBackData,
        );
        return show(config('status.success'), "订单详情", $data);
    }
}
